/**
 * @author ydiego
 * @date on 2020/08/04
 * @email yudeguo@hzxituan.com
 */
const connection = require('../../db/mysql');
const initSql = require('../tool/index')

function query(sql) {
  return new Promise(resolve => {
    connection.query(sql, (error, results, fields) => {
      if (error) {
        resolve({
          message: 'query error',
          success: false,
          data: error
        })
      }

      resolve({
        message: '',
        success: true,
        data: results
      })

    })
  })
}

const getLog = async ctx => {
  try {
    const { sql = '' } = ctx.request.body
    const res = await query( sql || 'select * from xt_buried_point_process limit 100');
    console.log(res)
    ctx.body = res
  } catch (e) {
    ctx.body = {
      message: 'something went wrong',
      success: false,
      data: e
    }
  }
}


const getPUData = async ctx => {
  try {
    const sql = initSql(ctx.request.body)
    ctx.body = await query(sql);
  } catch (e) {
    ctx.body = {
      message: 'something went wrong',
      success: false,
      data: null
    }
  }
}


module.exports = {
  getLog,
  getPUData,
}